package com.java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.java.domain.event.ActivityUserEvent;
import com.java.service.RecordService;
import com.java.service.UserService;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired private UserService userService;
	@Autowired private RecordService recordService;

	@RequestMapping(value="/signin", method = RequestMethod.GET)
	public String signin() {
		if(ServletUtil.userIsLogin()) {
			return "redirect:/user/account";
		}
		return "user/signin";
	}

	@RequestMapping(value="/signup", method = RequestMethod.GET)
	public String signup() {
		if(ServletUtil.userIsLogin()) {
			return "redirect:/user/account";
		}
		return "user/signup";
	}

	@RequestMapping(value="/logon", method = RequestMethod.POST)
	public String logon(String account, String password, Model model) {
		if("".equals(account)) {
			model.addAttribute("error", "Account is empty");
			return "user/signin";
		}
		if("".equals(password)) {
			model.addAttribute("error", "Password is empty");
			return "user/signin";
		}
		BaseRespone res = userService.login(account, password);
		if(res.getStatus() == 500) {
			model.addAttribute("error", res.getMsg());
			return "user/signin";
		}
		return "redirect:/user/account";
	}

	@RequestMapping(value="/register", method = RequestMethod.POST)
	public String register(String account, String password, Integer roleType, String name, String mobile, Model model) {
		if("".equals(account)) {
			model.addAttribute("error", "Account is empty");
			return "user/signup";
		}
		if("".equals(password)) {
			model.addAttribute("error", "Password is empty");
			return "user/signup";
		}
		if("".equals(name)) {
			model.addAttribute("error", "Name is empty");
			return "user/signup";
		}
		if("".equals(mobile)) {
			model.addAttribute("error", "Mobile is empty");
			return "user/signup";
		}
		BaseRespone res = userService.register(account, password, roleType, name, mobile);
		if(res.getStatus() == 500) {
			model.addAttribute("error", res.getMsg());
			return "user/signup";
		}
		model.addAttribute("success", res.getMsg());
		return "user/signup";
	}

	@RequestMapping(value="/logout", method = RequestMethod.GET)
	public String logout() {
		ServletUtil.getSession().invalidate();
		return "user/signin";
	}

	
	/**
	 *  Main Function
	 */


	@RequestMapping(value="/account", method = RequestMethod.GET)
	public String account(Model model) {
		String userId = ServletUtil.getUserId();
		ActivityUserEvent user = userService.getUserById(userId);
		model.addAttribute("user", user);
		return "user/account";
	}

	@RequestMapping(value="/password", method = RequestMethod.GET)
	public String password(Model model) {
		String userId = ServletUtil.getUserId();
		ActivityUserEvent user = userService.getUserById(userId);
		model.addAttribute("user", user);
		return "user/password";
	}

	@RequestMapping(value="/order", method = RequestMethod.GET)
	public String order() {
		
		return "user/order";
	}

	@RequestMapping(value="/review", method = RequestMethod.GET)
	public String review() {
		
		return "user/review";
	}
	
	@GetMapping(value = "/orders")
	public TableRespone orders(int page, int size) {
		return recordService.getList(ServletUtil.getUserId(), page, size);
	}
}
